package com.caht.Exception.GlobalExceptionHandler;

import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 全局异常处理
 */
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler implements HandlerInterceptor {
    @ExceptionHandler(ExpiredJwtException.class)
    public String handleException(Exception e) {
        log.info(e.getMessage());
        if(e.getMessage().contains("JWT expired")){
            return "Token过期，请重新登录";
        }else {
            return "未知错误";
        }
    }
}
